package com.example.student.controller;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import com.example.student.constant.ResultCode;
import com.example.student.constant.StaticPath;
import com.example.student.constant.TemplatePath;
import com.example.student.entity.ResultDO;
import com.example.student.entity.UserDO;
import com.example.student.service.UserInterService;

/**
 * 用户登录页面控制类
 *
 * @author 
 */
@Controller
@ComponentScan({"com.example.student.service"})
@RequestMapping("/user")
public class UserController{

    @Resource
    private UserInterService userService;

   
    @GetMapping("login")
    public String showLogin() {
        return TemplatePath.USER_LOGIN;
    }

   
    @PostMapping("login")
    public String login(@ModelAttribute UserDO userDO) {
        if (StringUtils.isEmpty(userDO.getLoginName()) || StringUtils.isEmpty(userDO.getPassword())) {
            return "redirect:/" + StaticPath.ERROR + "?" + ResultCode.MSG_PARAMETER_INVALID;
        }

        ResultDO<Void> result = userService.login(userDO);
        if (!result.isSuccess()) {
            return "redirect:/" + StaticPath.ERROR + "?" + result.getMsg();
        }

        return "redirect:/" + TemplatePath.MAIN_HTML;
    }

}
